using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 追击状态类，处理敌人在追击状态下的行为
/// </summary>
public class ChaseState : IState
{
    private Enemy enemy; // 当前的敌人对象

    /// <summary>
    /// 构造函数，初始化敌人对象
    /// </summary>
    /// <param name="enemy">当前的敌人对象</param>
    public ChaseState(Enemy enemy)
    {
        this.enemy = enemy;
    }

    /// <summary>
    /// 当进入追击状态时调用，播放追击动画
    /// </summary>
    public void OnEnter()
    {
        enemy.parameters.enemyAnimator.Play("Chase");
    }

    /// <summary>
    /// 固定更新方法，目前未实现
    /// </summary>
    public void OnFixedUpdate()
    {
        throw new System.NotImplementedException();
    }

    /// <summary>
    /// 每帧更新方法，判断是否进入攻击范围
    /// </summary>
    public void OnUpdate()
    {
        IsAttack();
    }

    /// <summary>
    /// 当退出追击状态时调用，目前未实现
    /// </summary>
    public void OnExit()
    {
        throw new System.NotImplementedException();
    }

    /// <summary>
    /// 判断玩家是否在攻击范围内，如果在则转换为攻击状态
    /// </summary>
    public void IsAttack()
    {
        if (!enemy.target) return; // 判断追击目标是否为空
        float distance = Vector2.Distance(enemy.transform.position, enemy.target.position); // 计算敌人与玩家的距离
        if (distance < enemy.parameters.enemyAttackDistance) // 判断是否在攻击距离内
        {
            enemy.TransitionState(StateType.Attack); // 转换为攻击状态
        }
    }
}